package org.duoyi.striver.consts;

import cn.hutool.core.net.URLDecoder;
import cn.hutool.core.util.StrUtil;
import com.develop.jawin.COMException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.duoyi.jdm.Dm.DmInstant;
import org.duoyi.striver.global.DmManager;
import org.duoyi.striver.model.Point;
import org.duoyi.striver.model.impl.PointImpl;
import org.duoyi.striver.model.impl.Region;
import org.duoyi.striver.model.impl.Sleeper;

import java.nio.charset.StandardCharsets;

/**
 * 截图
 *
 * @author chenyuheng create class on 2021-05-08
 */
@Slf4j
@AllArgsConstructor
public enum Picture {

    //-----辅助线程
    邀请入队(Region.in(463, 280, 566, 317), "邀请入队"),
    活跃礼盒(Region.in(828, 624, 910, 653), "活跃礼盒"),
    //-----辅助线程

    无界面_标识(Region.in(286, 23, 305, 47), "无界面_标识"),
    退出界面(Region.in(462, 465, 565, 513), "退出界面"),
    挂机界面(Region.in(241, 513, 330, 560), "挂机界面"),
    日程界面(Region.in(430, 118, 587, 149), "日程界面"),

    右下角菜单_长(Region.in(752, 697, 780, 730), "右下角菜单_长"),

    战斗_自动按钮(Region.in(960, 703, 1003, 728), "战斗_自动按钮"),
    战斗_取消自动战斗(Region.in(964, 697, 999, 733), "战斗_取消自动战斗"),
    战斗_失败标识(Region.in(463, 135, 564, 168), "战斗_失败标识"),

    系统商店界面(Region.in(754, 307, 919, 431), "系统商店界面"),

    逛摊购物_未激活(Region.in(942, 279, 971, 363), "逛摊购物_未激活"),
    逛摊购物_已激活(Region.in(947, 276, 983, 359), "逛摊购物_已激活"),
    逛摊购物_摊位未激活(Region.in(146, 130, 233, 156), "逛摊购物_摊位未激活"),
    逛摊购物_摊位已激活(Region.in(147, 130, 231, 155), "逛摊购物_摊位已激活"),

    宠物商店_标识(Region.in(805, 501, 870, 530), "宠物商店_标识"),

    背包_标识(Region.in(953, 145, 985, 229), "背包_标识"),

    NPC_对话框(Region.in(938, 373, 982, 426), "NPC_对话框"),

    //宝图任务
    宝图任务_未完成(Region.in(292, 180, 363, 238), "宝图任务_未完成"),
    宝图任务_已完成(Region.in(291, 175, 361, 239), "宝图任务_已完成"),
    宝图任务_掌柜对话(Region.in(733, 422, 846, 453), "宝图任务_掌柜对话"),
    宝图任务_掌柜结束(Region.in(717, 161, 829, 268), "宝图任务_掌柜结束"),

    //挖图
    背包_宝图(Region.in(542, 165, 926, 553), "背包_宝图"),
    背包_宝图选择(Region.in(20, 19, 947, 724), "背包_宝图选择"),
    挖图_宝图(Region.in(837, 520, 899, 580), "挖图_宝图"),

    //竞技场
    竞技场_未完成(Region.in(663, 174, 722, 237), "竞技场_未完成"),
    竞技场_已完成(Region.in(670, 179, 717, 237), "竞技场_已完成"),
    竞技场_界面标识(Region.in(644, 615, 908, 649), "竞技场_界面标识"),

    //伙伴任务
    伙伴任务_未完成(Region.in(137, 456, 876, 596), "伙伴任务_未完成"),
    伙伴任务_百晓通对话标识(Region.in(665, 167, 828, 237), "伙伴任务_百晓通对话标识"),
    伙伴任务_任务面板_选中(Region.in(118, 176, 389, 629), "伙伴任务_任务面板_选中"),
    伙伴任务_任务面板_未选中(Region.in(118, 176, 389, 629), "伙伴任务_任务面板_未选中"),
    伙伴任务_任务列表标识(Region.in(697, 645, 823, 684), "伙伴任务_任务列表标识"),
    伙伴任务_150点任务(Region.in(231, 324, 798, 637), "伙伴任务_150点任务"),
    伙伴任务_50点任务(Region.in(231, 324, 798, 637), "伙伴任务_50点任务"),
    伙伴任务_对话(Region.in(499, 709, 932, 760), "伙伴任务_对话"),
    //    伙伴任务_给予物品(Region.in(), "伙伴任务_给予物品"),
    伙伴任务_小型战斗对话(Region.in(784, 436, 977, 462), "伙伴任务_小型战斗对话"),

    //英雄试炼
    英雄试炼_已完成(Region.in(790, 177, 840, 239), "英雄试炼_已完成"),
    英雄试炼_高老庄_未完成标识(Region.in(640, 368, 669, 398), "英雄试炼_高老庄_未完成标识"),
    英雄试炼_东海龙宫_未完成标识(Region.in(636, 430, 661, 459), "英雄试炼_东海龙宫_未完成标识"),
    英雄试炼_天庭_未完成标识(Region.in(651, 238, 678, 270), "英雄试炼_天庭_未完成标识"),
    英雄试炼_火焰山_未完成标识(Region.in(631, 363, 657, 397), "英雄试炼_火焰山_未完成标识"),
    英雄试炼_流沙河_未完成标识(Region.in(594, 393, 624, 422), "英雄试炼_流沙河_未完成标识"),
    英雄试炼_狮驼岭_未完成标识(Region.in(647, 362, 677, 393), "英雄试炼_狮驼岭_未完成标识"),
    英雄试炼_花果山_未完成标识(Region.in(656, 384, 683, 413), "英雄试炼_花果山_未完成标识"),
    英雄试炼_宝箱(Region.in(248, 178, 909, 686), "英雄试炼_宝箱"),
    英雄试炼_异域挑战令(Region.in(955, 418, 977, 459), "英雄试炼_异域挑战令"),
    ;


    private final Region region;

    private final String path;

    public boolean find() {
        return this.find(0.8);
    }

    public boolean find(double sim) {
        String result = findPic(sim);
        return StrUtil.isNotBlank(result);
    }

    public Point findPoint() {
        return findPoint(0.8d);
    }

    public Point findPoint(double sim) {
        String result = findPic(sim);
        if (result == null) return null;

        if (StrUtil.isNotBlank(result)) {
            String[] split = result.split("\\|")[0].split(",");
            return PointImpl.on(Integer.parseInt(split[1]), Integer.parseInt(split[2]));
        }

        return null;
    }

    private String findPic(double sim) {
        DmInstant dmInstant = DmManager.get();
        String result = null;
        String picPath = URLDecoder.decode(this.getClass().getClassLoader().getResource("picture/" + this.path + ".bmp").getPath(), StandardCharsets.UTF_8);
        if (picPath.startsWith("/")) {
            picPath = picPath.substring(1);
        }

        try {
            dmInstant.MoveTo(0, 0);
            Sleeper.sleep();
            result = dmInstant.FindPicEx(region.getX1() - 10, region.getY1() - 10, region.getX2() + 10, region.getY2() + 10, picPath, "000000-000000", sim, 0);
        } catch (COMException e) {
            log.error("找图【{}】失败", this.name());
        }
        return result;
    }

}
